package pingce.lucene.sentiment;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class IndexSentimentWords {

	public static void indexSentimentWords(String sourceFile, String indexDir, String polarity)throws Exception{
		Directory dir=FSDirectory.open(new File(indexDir));
		Analyzer whiteSpaceAnalyzer = new WhitespaceAnalyzer(Version.LUCENE_31);
		IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_31,whiteSpaceAnalyzer);
		IndexWriter writer=new IndexWriter(dir,iwc);
		BufferedReader br = new BufferedReader(
				new InputStreamReader(new FileInputStream(sourceFile), "utf-8"));
		String word;
		while((word = br.readLine()) != null){
			if(word.length() > 0){
				Document doc = new Document();
				String spaceWord = Utility.toSpaceString(word);
				doc.add(new Field("word", spaceWord, 
						Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES));
				doc.add(new Field("polarity", polarity, 
						Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES));
				writer.addDocument(doc);
			}
		}
		writer.commit();
		writer.close();
	}
	
	public static void deleteFile(String filePath){
		try{
			File file = new File(filePath);
			if(file.exists()){
				if(file.isDirectory()){
					for(File f : file.listFiles()){
						deleteFile(f.getAbsolutePath());
					}
				}else{
					file.delete();
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args)throws Exception{
		deleteFile("lucene/polarity");
		String sourceFile1 = "sentiment-dictionary/negative-sort.txt";
		String sourceFile2 = "sentiment-dictionary/positive-sort.txt";
		String indexDir = "lucene/polarity";
		String polarity1 = "NEG";
		String polarity2 = "POS";
		indexSentimentWords(sourceFile1, indexDir, polarity1);
		indexSentimentWords(sourceFile2, indexDir, polarity2);
	}
}

